<style>
    .layui-form-item .layui-inline {
        margin-top: 10px;
    }
    .layui-form-label{
        width: 100px;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form" lay-filter="LAY-list-form">
            <div id="view"></div>
        </div>
    </div>
</div>

<script id="page1" type="text/html">
    <input type="hidden" name="tijiao_type" value="{{layui.router().search.type ||''}}">
    <div class="layui-card-header">店仓入库单资料</div>
    <div class="layui-card-body">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">仓库：</label>
                <div class="layui-input-inline layui-jy-width200">
                    <input type="hidden" name="base_warehouse_id" value="{{d.base_warehouse_id}}">
                    <input type="hidden" name="base_warehouse_name" value="{{d.base_warehouse_name}}">
                    <span class="layui-input layui-input-none">{{d.base_warehouse_name||''}}</span>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">入库批次号：</label>
                <div class="layui-input-inline layui-jy-width200">
                    <input type="hidden" name="batch" value="{{d.batch}}">
                    <span class="layui-input layui-input-none">{{d.batch||''}}</span>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">入库业务单号：</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="business_sn" value="{{d.business_sn}}">
                    <span class="layui-input layui-input-none">{{d.business_sn||''}}</span>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">入库类型：</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="type" value="{{d.type}}">
                    <span class="layui-input layui-input-none">{{d.type_name||''}}</span>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">状态：</label>
                <div class="layui-input-inline">
                    <span class="layui-input layui-input-none">{{d.status_name||''}}</span>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-card-header">商品信息</div>
    <div class="layui-card-body">
        <div class="layui-tab">
            <ul class="layui-tab-title">
                <li class="layui-this">明细信息</li>
            </ul>
            <div class="layui-tab-content">
                <div class="tab1 layui-tab-item layui-show">
                    <div class="layui-item">
                        <!--<button class="export1 layui-btn">导出</button>-->
                    </div>
                    <table id="LAY-list" lay-filter="LAY-list"></table>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-card-body layui-jy-center">
        <input type="hidden" name="id" value="{{layui.router().search.id}}">
        <a lay-href="Shopwarehousein/getList" class="layui-btn">返回</a>
        {{# if(layui.router().search.type == 1||!layui.router().search.id) { }}
        <input type="button" lay-submit lay-filter="LAY-submit-1" value="确定入库" class="layui-btn LAY-submit1">
        {{#} }}
    </div>

</script>
<!-- 行内工具 -->
<script type="text/html" id="LAY-list-toolbar">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="del" href="javascript:;">删除</a>
</script>
<script>
    layui.use(['admin', 'form', 'toolJ', 'laytpl', 'laydate', 'upload', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , laytpl = layui.laytpl
            , setter = layui.setter
            , table = layui.table
            , toolJ = layui.toolJ;
        var id = layui.router().search.id;
        var type = layui.router().search.type;
        function tableView(data) {
            var url = '';
            var jdata = '';
            if (data.length > 0) {
                jdata = data;
            }
            table.render({
                elem: '#LAY-list'
                , url: url
                , data: jdata
                , toolbar: true
                , defaultToolbar: ['exports', 'filter', 'print']
                , cellMinWidth: 80
                , cols: [[
                    {field: 'sku', title: 'SKU'}
                    , {field: 'sku_name', title: 'SKU名称'}
                    , {field: 'quantity', title: '数量'}
                    , {field: 'quantity_in', title: '入库数量*', edit: 'text'}
                    , {field: 'price', title: '单价'}
                    , {field: 'remark', title: '备注*', edit: 'text'}
                ]]
                , page: false
                , limit: 1000
                ,text:{none: '暂无相关数据!' }
                ,done: function(res, curr, count){
                    var tableView = this.elem.next(); // 当前表格渲染之后的视图
                    if (type == 2){
                        layui.each(res.data, function(i, item){
                            tableView.find('tr[data-index=' + i + ']').find('td').data('edit',false) // 取消编辑功能
                        });
                    }
                }
            })
        }


        function viewHtml(data) {
            var getTpl = page1.innerHTML
                , view = $('#view');
            laytpl(getTpl).render((data.info)[0], function (html) {
                view.html(html);
            });
            tableView(data.detail);
            table.render(null, 'LAY-list');
            form.render(null, 'LAY-list-form');
            //监听提交
            form.on('submit(LAY-submit-1)', function (data) {
                if (typeof table.cache['LAY-list'] != 'undefined' && table.cache['LAY-list'].length > 0) {
                    data.field.list = JSON.stringify(table.cache['LAY-list']);
                    toolJ.ajax({"data": data.field,"url":""},function (data) {
                        if (data.code == 1){
                            layer.alert(data.msg);
                            window.location.href = "/#/Shopwarehousein/getList";
                        }
                    });
                } else {
                    toolJ.error('不存在明细!');
                }
            });
        }

        /*监听编辑数据*/
        table.on('edit(LAY-list)', function (obj) {
            if (type == 2){
                setTimeout(function () {
                    obj.update({quantity_in: obj.data.original_quantity_in,"remark":obj.data.remark});
                }, 100);
                return false;
            }
            var that = this;
            var tdElem = $(that).closest('td');
            var field = obj.field;
            var value = obj.value;
            if (field === 'quantity_in') {
                setTimeout(function () {
                    var reg=/^([1-9]\d*|[0]{1,1})$/; //含0正整数
                    if(!reg.test(value)){
                        layer.msg('入库数量填写有误!', {anim: 6});
                        obj.update({quantity_in: obj.data.original_quantity_in});
                        tdElem.click();
                        return false;
                    }else {
                        obj.data['is_save'] = true;
                        toolJ.ajax({"data":obj.data,"url":"/index/Shopwarehousein/edit"},function (res) {
                            var icon = 1;
                            if (res.code != 1) icon = 2;
                            layer.msg(res.msg, {icon: icon});
                            $('.search').click();
                        });
                    }
                }, 100);
            }

            if (field === 'remark') {
                setTimeout(function () {
                    obj.data['is_save'] = true;
                    toolJ.ajax({"data":obj.data,"url":"/index/Shopwarehousein/edit"},function (res) {
                        var icon = 1;
                        if (res.code != 1) icon = 2;
                        layer.msg(res.msg, {icon: icon});
                        $('.search').click();
                    });
                }, 100);
            }
        });

        if (id) {
            toolJ.ajax({"data": {"id": id}, "type": "get"},
                function (data) {
                    viewHtml({"info": data.data.info, "detail": data.data.detail});
                }
            );
        }

    })
</script>